// pages/view/ip/SettlementIncome/SettlementIncome.js
var wxCharts = require('../../../../utils/wxcharts.js');
const network = require('../../../../utils/network.js')
var app = getApp();
var columnChart = null;
var lineChart = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectDate:'2018-01',
    chartTitle_1: '住院结算收入分析图-日期分析',
    chartTitle_2: '住院结算收入分析图-科室分析',

    //日期分析-折线图
    date_list: [],
    date_data: [],
    
    //科室分析-柱形图
    dept_categories: [],
    dept_data: [],


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    that.myRequest(options)
   
  },
  /**
 * 调用封装好的网络请求，获取数据
 */
  myRequest: function (options) {
    var that = this
    console.log('options:', options)
    //调用封装好的网络请求，获取数据
    network.get('/ip/income?selectTime=' + options.selectTime, {}).then(function (res) {
      console.log('res:', res)
      var obj1 = res.data.data.list1
      var obj2 = res.data.data.list2
      var obj3 = res.data.data_2.list1
      var obj4 = res.data.data_2.list2
      that.setData({

        //日期分析-折线图
        date_list: obj1,
        date_data: obj2,

        //科室分析-柱形图
        dept_categories: obj3,
        dept_data: obj4,

      })
    }).then(function (res) {
      wx.hideLoading()
      that.createColumnChart();
      that.createLineChart();

    }).catch(function (res) {
      console.log(res)
    })
  },
  createColumnChart: function (e) {
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: that.data.dept_categories,
      series: [{
        name: '金额',
        data: that.data.dept_data,
        format: function (val, name) {

          return val.toFixed(2) + '（元）';
        }
      }],
      yAxis: {
        format: function (val) {
          return val + '';
        },
        title: '金额（元）',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 200,
    });
  },


  createLineChart: function () {
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    // var simulationData = this.createSimulationData();
    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: that.data.date_list,
      animation: true,
      // background: '#f5f5f5',
      series: [{
        name: '金额',
        data: that.data.date_data,
        format: function (val, name) {
          return val.toFixed(2) + '';
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '金额（元）',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: windowWidth,
      height: 200,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})